package leetcode.D400.T394;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @File Info: leetcode -- <Solution>
 * @Author: DYZ
 * @Create: 2022-04-13 20:28:12
 */
class Solution {
    public String decodeString(String s) {
        Deque<Integer> numStack = new LinkedList<>();
        Deque<String> strStack = new LinkedList<>();
        int num = 0;
        StringBuilder res = new StringBuilder();
        for (char c : s.toCharArray()) {
            if (c == '[') {
                numStack.push(num);
                num = 0;
                strStack.push(res.toString());
                res = new StringBuilder();
            } else if (c == ']') {
                int tnum = numStack.pop();
                StringBuilder tmpSb = new StringBuilder();
                for (int j=0; j<tnum; ++j)
                    tmpSb.append(res);
                res = new StringBuilder(strStack.pop() + tmpSb);
            } else if (c >= '0' && c <= '9') {
                num = num * 10 + c - '0';
            } else
                res.append(c);
        }
        return res.toString();
    }
}
